package com.chixing.day13_list;

import java.util.*;

public class ListDemo {
    public static void main(String[] args) {
        ArrayList<String> list =  new ArrayList<>();
        list.add("hello");
        list.add("hello");
        list.add("big");
        list.add("small");
        // code
        list.add("good");
        list.add("money");
        list.add(4,"code");

        list.remove(2);  // 返回 被删除位置上的元素值
        list.remove("money");// 返回  true/false
        list.size();// 获得元素的个数
       // list.clear();

        list.get(4);
        list.indexOf("build");
        list.set(4,"edit");

        //遍历
        Iterator<String> it = list.iterator();
        while(it.hasNext()){
            String ele = it.next();
            System.out.println(ele);
        }



        //LinkedList
//        （1）添加2个学生对象
//           (2) 获得元素个数
//           (3) 添加一个新学生在第一个位置上
//           (4) 获得名称为"katy" 的学生，对象
//           (5) 迭代遍历集合
//           (6) 获得第2个学生
//           (7) 删除第1个学生对象
//           (8)将最后一个学生的年龄改为22
        LinkedList<Student> linkedList = new LinkedList<>();
        Student student1 = new Student("smith",20);
        Student student2 = new Student("katy",22);
        linkedList.add(student1);
        linkedList.add(student2);

        linkedList.size();

        Student student3 = new Student("wade",23);
        linkedList.addFirst(student3);

        Iterator<Student> iterator = linkedList.iterator();
        while (iterator.hasNext()){
            Student student = iterator.next();
            if (student.getStuName().equals("katy"))
                System.out.println(student);
        }

        System.out.println(linkedList.get(1));

        linkedList.getLast().setStuAge(22);
        System.out.println(linkedList.getLast());
        System.out.println(  "===========");
        Iterator<Student> iterator1 = linkedList.iterator();
        while (iterator1.hasNext()){
            Student student = iterator1.next();
            System.out.println(student);
        }

        System.out.println(  "===========");
        System.out.println( "peek: 默认获得第一个元素：" + linkedList.peek());
        System.out.println("pop:默认获得第一个元素" +  linkedList.pop());


    }
}


class MyArrayList<E>{
    private Object[] defaultArray ;

    public MyArrayList(int initCapacity){
        defaultArray = new Object[initCapacity];
    }
}